using System.Collections.Generic;
using OracleInternal.BulkCopy;

namespace OracleInternal.TTC
{
	internal class DirectPathStream
	{
		internal List<DirectPathRow> m_dpRows = new List<DirectPathRow>();

		internal List<BulkCopyBindingRow> m_bindingRows = new List<BulkCopyBindingRow>();

		internal DirectPathRow m_dpRowToAdd;

		internal BulkCopyBindingRow m_bindingRowToAdd;

		internal int m_streamSize;

		internal void Reset()
		{
			DirectPathRow.AddToCache(m_dpRows);
			m_dpRows.Clear();
			BulkCopyBindingRow.AddToCache(m_bindingRows);
			m_bindingRows.Clear();
			m_streamSize = 0;
			m_dpRowToAdd = null;
			m_bindingRowToAdd = null;
		}

		internal void ResetForNextStream()
		{
			DirectPathRow.AddToCache(m_dpRows);
			m_dpRows.Clear();
			BulkCopyBindingRow.AddToCache(m_bindingRows);
			m_bindingRows.Clear();
			m_streamSize = 0;
			if (m_dpRowToAdd != null && m_bindingRowToAdd != null)
			{
				m_dpRows.Add(m_dpRowToAdd);
				m_bindingRows.Add(m_bindingRowToAdd);
				m_streamSize += m_dpRowToAdd.Size;
				m_dpRowToAdd = null;
				m_bindingRowToAdd = null;
			}
		}

		internal bool Add(DirectPathRow dpRow, BulkCopyBindingRow bindingRow)
		{
			if (m_streamSize + dpRow.Size <= 131072)
			{
				m_dpRows.Add(dpRow);
				m_bindingRows.Add(bindingRow);
				m_streamSize += dpRow.Size;
				return true;
			}
			m_dpRowToAdd = DirectPathRow.SplitRow(131072 - m_streamSize, dpRow);
			m_bindingRowToAdd = bindingRow.Copy();
			m_dpRows.Add(dpRow);
			m_bindingRows.Add(bindingRow);
			m_streamSize += dpRow.Size;
			return false;
		}
	}
}
